﻿
using System;
using System.Collections.Generic;
using Cysharp.Threading.Tasks;
using UnityEngine;
using WssUtils;

namespace JQMergeFramework
{
    public class CommonFun
    {
        //延迟执行方法
        public static async UniTask DelayCallback(Action call, float time = 1f)
        {
            await UniTask.Delay(TimeSpan.FromSeconds(time));
            call?.Invoke();
        }
        
        
        //获取路径长度
        public static float CalculateTotalLength(Vector3[] path)
        {
            float totalLength = 0;
            for (int i = 0; i < path.Length - 1; i++)
            {
                totalLength += Vector3.Distance(path[i + 1], path[i]);
            }
            return totalLength;
        }

        //获取棋盘随机生成房子的位置
        public static List<Vector2Int> GetVector2List(GroundBase[,] groundArray,int row = 5,int col = 5,int size =3)
        {
            List<Vector2Int> cache = new List<Vector2Int>();
            
            int RmNum = size;
            for (int i = 0; cache.Count < RmNum; i++)
            {
                Vector2Int  nValue = new Vector2Int(MyRandom.Range(0, row),MyRandom.Range(0, col));
                if (!groundArray[nValue.x,nValue.y].IsDisabled)
                {
                    if (!cache.Contains(nValue))
                    {
                        cache.Add(nValue);
                    }
                }
            }
            return cache;
        }
        
        
        #region 六边形判断相关
        public static Vector2Int GetNear(int x,int y,HexGrid.Dir dir)
        {
            //2，1
            bool up = x % 2 != 1;
            int nx, ny;
            nx = x;ny = y;
            switch (dir)
            {
                case HexGrid.Dir.RightTop:
                    nx += 1;
                    if(up)
                        ny += 1;
                    break;
                
                case HexGrid.Dir.Top:
                    ny += 1;
                    break;
           
                case HexGrid.Dir.RightBottom:
                    nx += 1;
                    if (!up)
                        ny -= 1;
                    break;
                case HexGrid.Dir.Bottom:
                    ny -= 1;
                    break;
                case HexGrid.Dir.LeftBottom:
                    nx -= 1;
                    if (!up)
                        ny -= 1;
                    break;
                case HexGrid.Dir.LeftTop:
                    nx -= 1;
                    if (up)
                        ny += 1;
                    break;
                default:
                    break;
            }
            return new Vector2Int(nx, ny);
        }
        
        
        public static bool IsInRange(int x,int y)
        {
            return x >= 0 && x < 5 && y >= 0 && y < 5;
        }
        #endregion


        public static float GetDistance(Vector3[] v3Ary)
        {
            float dis = 0;
            for (int i = 1; i < v3Ary.Length; i++)
            {
                dis += Vector3.Distance(v3Ary[i - 1], v3Ary[i]);
            }
            return dis;
        }

        public static float GetScreenRatio()
        {
            float targetHight = 1920.0f;
            if (1080*Screen.height>1920*Screen.width)
            {
                targetHight = 1080f * Screen.height / Screen.width;
            }

            float f = targetHight / 1920.0F;
            return f;
        }
    }
}